package com.ruoyi.common.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CronExpression {
    public static List<String> getCron(String cycletype,Integer cyclerate,Integer cyclenum) {
        List<String> listcron = new ArrayList<>();
        if (cycletype.equals("day")) {
            //日
            int time = 0;
            if (24 % cyclerate == 0) {
                time = 24 / cyclerate;
            } else {
                time = 24 / cyclerate + 1;
            }

            if (time == 24) {
                if(cyclenum==7){
                    listcron.add("0 1 0 4/" + cyclenum + " * ?");
                }else {
                    listcron.add("0 0 0 1/" + cyclenum + " * ?");
                }
            } else {
                listcron.add("0 0 0/" + time + " 1/" + cyclenum + " * ?");
            }
        } else if (cycletype.equals("week")) {
            //周
            int time = 0;
            if (7 % cyclerate == 0) {
                time = 7 / cyclerate;
            } else {
                time = 7 / cyclerate + 1;
            }

            List<Integer> weeks = new ArrayList<>();
            for (int i = 0; i < cyclerate; i++) {
                Integer weeknum = 1 + i * time;
                if (weeknum <= 7) {
                    if (weeknum == 1) {
                        weeks.add(2);
                    } else if (weeknum == 2) {
                        weeks.add(3);
                    } else if (weeknum == 3) {
                        weeks.add(4);
                    } else if (weeknum == 4) {
                        weeks.add(5);
                    } else if (weeknum == 5) {
                        weeks.add(6);
                    } else if (weeknum == 6) {
                        weeks.add(7);
                    } else if (weeknum == 7) {
                        weeks.add(1);
                    }
                }
            }

            List<Integer> listcyclenum=new ArrayList<>();
            if(cyclenum==1){
                listcyclenum.add(1);
                listcyclenum.add(2);
                listcyclenum.add(3);
                listcyclenum.add(4);
                listcyclenum.add(5);
            }else if(cyclenum==2){
                listcyclenum.add(1);
                listcyclenum.add(3);
                listcyclenum.add(5);
            }else if(cyclenum==3){
                listcyclenum.add(1);
                listcyclenum.add(4);
            }else if(cyclenum==4){
                listcyclenum.add(1);
                listcyclenum.add(5);
            }else if(cyclenum==5){
                listcyclenum.add(1);
            }

            for(int n=0;n<listcyclenum.size();n++) {
                for (int i = 0; i < weeks.size(); i++) {
                    listcron.add("0 0 0 ? * " + weeks.get(i) + "#" + listcyclenum.get(n));
                }
            }
        } else if (cycletype.equals("month")) {
            //月
            int time = 0;
            if (31 % cyclerate == 0) {
                time = 31 / cyclerate;
            } else {
                time = 31 / cyclerate + 1;
            }

            List<String> months = new ArrayList<>();
            for (int i = 0; i < cyclerate; i++) {
                if ((1 + i * time) <= 31) {
                    months.add(String.valueOf(1 + i * time));
                }
            }
            if (months.size() > 0) {
                String strmonth = String.join(",", months.toArray(new String[months.size()]));
                listcron.add("0 0 0 " + strmonth + " /" + cyclenum + " ?");
            }
        } else if (cycletype.equals("year")) {
            //年
            int time = 0;
            if (12 % cyclerate == 0) {
                time = 12 / cyclerate;
            } else {
                time = 12 / cyclerate + 1;
            }

            List<String> months = new ArrayList<>();
            for (int i = 0; i < cyclerate; i++) {
                if ((1 + i * time) <= 12) {
                    months.add(String.valueOf(1 + i * time));
                }
            }
            if (months.size() > 0) {
                String strmonth = String.join(",", months.toArray(new String[months.size()]));
                listcron.add("0 0 0 1 " + strmonth + " ? /" + cyclenum);
            }
        }
        return listcron;
    }

    public static Date getEndTime(String cycletype, Integer cyclerate, Integer cyclenum, Date workstarttime) {
        Date workendtime=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        if (cycletype.equals("day")) {
            //日
            int time = 0;
            if (24 % cyclerate == 0) {
                time = 24 / cyclerate;
            } else {
                time = 24 / cyclerate + 1;
            }

            if (time == 24) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(workstarttime);
                cal.add(Calendar.HOUR, (cyclenum-1)*24+24);
                workendtime=cal.getTime();
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(workstarttime);
                cal.add(Calendar.HOUR, (cyclenum-1)*24+time);
                workendtime=cal.getTime();
            }
        } else if (cycletype.equals("week")) {
            //周
            int time = 0;
            if (7 % cyclerate == 0) {
                time = 7 / cyclerate;
            } else {
                time = 7 / cyclerate + 1;
            }

            List<Integer> weeks = new ArrayList<>();
            for (int i = 0; i < cyclerate; i++) {
                Integer weeknum = 1 + i * time;
                if (weeknum <= 7) {
                    if (weeknum == 1) {
                        weeks.add(2);
                    } else if (weeknum == 2) {
                        weeks.add(3);
                    } else if (weeknum == 3) {
                        weeks.add(4);
                    } else if (weeknum == 4) {
                        weeks.add(5);
                    } else if (weeknum == 5) {
                        weeks.add(6);
                    } else if (weeknum == 6) {
                        weeks.add(7);
                    } else if (weeknum == 7) {
                        weeks.add(1);
                    }
                }
            }

            List<Integer> listcyclenum=new ArrayList<>();
            if(cyclenum==1){
                listcyclenum.add(1);
                listcyclenum.add(2);
                listcyclenum.add(3);
                listcyclenum.add(4);
                listcyclenum.add(5);
            }else if(cyclenum==2){
                listcyclenum.add(1);
                listcyclenum.add(3);
                listcyclenum.add(5);
            }else if(cyclenum==3){
                listcyclenum.add(1);
                listcyclenum.add(4);
            }else if(cyclenum==4){
                listcyclenum.add(1);
                listcyclenum.add(5);
            }else if(cyclenum==5){
                listcyclenum.add(1);
            }

            for(int n=0;n<listcyclenum.size();n++) {
                for (int i = 0; i < weeks.size(); i++) {
                    //listcron.add("0 0 0 ? * " + weeks.get(i) + "#" + listcyclenum.get(n));
                }
            }
        } else if (cycletype.equals("month")) {
            //月
            int time = 0;
            if (31 % cyclerate == 0) {
                time = 31 / cyclerate;
            } else {
                time = 31 / cyclerate + 1;
            }

            Calendar cal = Calendar.getInstance();
            cal.setTime(workstarttime);
            cal.add(Calendar.DATE, (cyclenum-1)*31+time);
            workendtime=cal.getTime();
        } else if (cycletype.equals("year")) {
            //年
            int time = 0;
            if (12 % cyclerate == 0) {
                time = 12 / cyclerate;
            } else {
                time = 12 / cyclerate + 1;
            }

            Calendar cal = Calendar.getInstance();
            cal.setTime(workstarttime);
            cal.add(Calendar.MONTH, (cyclenum-1)*12+time);
            workendtime=cal.getTime();
        }
        return workendtime;
    }
}
